package com.zzptc.easyshop.dao;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.facory.DaoFacoryImp;
import com.zzptc.easyshop.vo.Good;
import com.zzptc.easyshop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class GoodTypeDaoImp implements IDAO {
    @Override
    public boolean insert(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean updete(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean delete(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean login(VO vo) throws SQLException {
        return false;
    }

    @Override
    public List getAll() {
        List list = new ArrayList();
        //获取数据库连接

        IDB db = new MysqlDB();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {

            conn = db.getConn();
            //获取工具类
            ps = conn.prepareStatement("select * from easybuy_product_category");
            rs = ps.executeQuery();
            while (rs.next()) {
                Good good = new Good();
                good.setEpc_id(rs.getInt(1));
                good.setEpc_name(rs.getString(2));
                good.setEpc_parent_id(rs.getInt(3));
                list.add(good);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                db.closeAll(conn,ps,rs);

            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return list;

    }

    public static void main(String[] args) throws Exception {
        List list = new ArrayList();
        GoodTypeDaoImp dao = (GoodTypeDaoImp) DaoFacoryImp.getInstance().createDAO("GoodTypeDaoImp");

        list = dao.getAll();
        System.out.println(list.size());
        //将list转换为JSON
        String j=JSON.toJSONString(list);
        System.out.println(j);
        //JSON转list
        List<Good> listALl=JSONObject.parseArray(j,Good.class);
        System.out.println(listALl.size());
    }
}
